package com.ossjk.qlh.components.controller;

import java.io.File;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ossjk.config.mvc.ResourceMappersProperties;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.exception.QlhRunTimeException;
import com.ossjk.core.system.api.IUploadCommonApi;
import com.ossjk.core.vo.ResponseBean;
import com.ossjk.qlh.system.dto.UploadDto;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * Copyright 2021 QLH. Tech Ltd. All rights reserved.
 * 
 * @Package: com.ossjk.qlh.components.controller
 * @ClassName: UploadComponentContrller
 * @Description: 上传组件-控制器
 * @author: Jason
 * @date: 2021年3月3日 下午2:31:47
 */
@Api(tags = "上传组件-控制器")
@RestController
@RequestMapping("/components/uploadComponent")
public class UploadComponentContrller extends BaseController {
	@Autowired
	private ResourceMappersProperties mappersProperties;

	@Autowired
	private IUploadCommonApi uploadCommonApi;

	@ApiOperation(value = "上传图片")
	@PostMapping("/imageUpload")
	public ResponseBean<String> imageUpload(MultipartFile file, UploadDto uploadDto) throws Exception {
		if (file.isEmpty()) {
			throw new QlhRunTimeException("uploadComponent-imageUpload 文件为空。");
		}
		if (mappersProperties == null && mappersProperties.getMappers() == null
				&& mappersProperties.getMappers().size() == 0) {
			throw new QlhRunTimeException("uploadComponent-imageUpload 文件保存路径配置为空。");
		}
		if (StrUtil.isEmpty(uploadDto.getStatisPath())) {
			throw new QlhRunTimeException("uploadComponent-imageUpload 组件statisPath配置为空。");
		}

		String url = uploadCommonApi.upload(file.getInputStream(), file.getOriginalFilename(),
				mappersProperties.getMappers().get(0).getFile(), uploadDto.getStatisPath(),
				mappersProperties.getMappers().get(0).getUri());
		return ResponseBean.Success("上传成功", url);
	}

}
